﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class MapEditorPanel : MonoBehaviour
{
    public Vector2 centerPos;

    public Transform root;
    
    [Header("预制体")] public GameObject perfab;


    public string prefix = "px";
    public List<string> fileNames;
    
    
    [Header("需要添加的名字")] public string addName="";
    [Header("需要添加的数据")] public AreaPanel addAreaPanel;
    
    
    
    /// <summary>
    /// 新建一个
    /// </summary>
    public void CreatAreaPanel()
    {
        //string id = prefix + addName + "_" + fileNames.Count;
        
        fileNames.Add(addAreaPanel.Id);//绑定
        areaPanelViews.Add(CreateAreaPanel(addAreaPanel)); //创建并添加Panel
    }
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="areaPanel"></param>
    public void RemoveAreaPanel(AreaPanelView areaPanelView)
    {
        if (areaPanelViews.Contains(areaPanelView))
        {
            areaPanelViews.Remove(areaPanelView);
            if (fileNames.Contains(areaPanelView.name))
            {
                fileNames.Remove(areaPanelView.name);
            }

            if (_dic.ContainsKey(areaPanelView.name))
            {
                _dic.Remove(areaPanelView.name);
            }
            areaPanelView.gameObject.SetActive(false);
            areaPanelView.name = "[delect]";
        }
    }


    #region "索引"
    
    public List<AreaPanelView> areaPanelViews = new List<AreaPanelView>();//视图
    
    public Dictionary<string,AreaPanelView> _dic = new Dictionary<string, AreaPanelView>();

    
    /// <summary>
    /// 将数据扫入
    /// </summary>
    public void AddAreaPanelViewDic()
    {
        _dic.Clear();
        foreach (var areaPanelView in areaPanelViews)
        {
            if (!_dic.ContainsKey(areaPanelView.GetAreaPanel.Id))
            {
                Debug.Log("存放数据"+ areaPanelView.GetAreaPanel.Id);
                _dic.Add(areaPanelView.GetAreaPanel.Id,areaPanelView);
            }
            areaPanelView.SetPanelViewDict(_dic);
        }
        
    }
    

    /// <summary>
    /// 设置关系
    /// </summary>
    public void SetPanelViewRelation()
    {
        foreach (var areaPanelView in areaPanelViews)
        {
            areaPanelView.SetPanelViewRelation();
        }
    }

    #endregion
    
    


    #region "存储文件"
    
    private List<AreaPanel> _areaPanels = new List<AreaPanel>();
    
    /// <summary>
    /// 读取所有
    /// </summary>
    public void ReadAll()
    {
        _areaPanels.Clear();
        AreaPanel areaPanelTemp = null;
        if (fileNames.Count > 0)
        {
            foreach (var fileName in fileNames)
            {
                areaPanelTemp = ReadAreaPanelFormFile(fileName);
                if(areaPanelTemp!=null)
                    _areaPanels.Add(areaPanelTemp);
            }
        }
        
        //1.创建指定对象
        for (int i = 0; i < _areaPanels.Count; i++)
        {
            AreaPanelView panelView = CreateAreaPanel(_areaPanels[i]);
            panelView.CreatePanelView(perfab);
            areaPanelViews.Add(panelView);
            panelView.Initialized();
        }
        Debug.Log("==创建完成=>");
        
        
        AddAreaPanelViewDic();//扫描入引用对象

        SetPanelViewRelation();//设置关系
        
        Debug.Log("==finsh=>");
    }

    
    
    /// <summary>
    /// 保存
    /// </summary>
    public void SaveFile()
    {
        _areaPanels.Clear();
        //将所有数据扫描出来
        foreach (var areaPanelView in areaPanelViews)
        {
            areaPanelView.UpdateInfo();
            _areaPanels.Add(areaPanelView.GetAreaPanel);//添加
        }
        
        for (int i = 0; i < _areaPanels.Count; i++)
        {
            SaveAreaPanel(_areaPanels[i].Id, _areaPanels[i]);
        }
        
    }
    
    

    AreaPanelView CreateAreaPanel(AreaPanel areaPanel)
    {
        GameObject areaPanelObj = new GameObject(areaPanel.Id);
        areaPanelObj.transform.SetParent(root);
        areaPanelObj.transform.position = areaPanel.basePos;//开始点
        AreaPanelView areaPanelView = areaPanelObj.AddComponent<AreaPanelView>();
        areaPanelView.SetAreaPanel(areaPanel,this,perfab);//设置数据
        return areaPanelView;
    }
    #endregion



    /// <summary>
    /// 清除所有
    /// </summary>
    public void ClearAll()
    {
        
    }




    /// <summary>
    /// 读取文件
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static AreaPanel ReadAreaPanelFormFile(string fileName)
    {
        AreaPanel panel = null;
        //读取json
        string path = Application.dataPath + "/" + fileName + ".json";
        if (File.Exists(path))
        {
            string str = File.ReadAllText(path);

            panel = JsonUtility.FromJson<AreaPanel>(str);
        }
        return panel;
    }


    /// <summary>
    /// 保存地图
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="areaPanel"></param>
    public static void SaveAreaPanel(string fileName,AreaPanel areaPanel)
    {
        string path = Application.dataPath + "/" + fileName + ".json";
        if (areaPanel == null)
        {
            Debug.LogError("保存对象为空:" + path);
            return;
        }
        string json = JsonUtility.ToJson(areaPanel);
        if (!File.Exists(path))
        {
            File.Create(path);
        }
        
        Debug.Log(json);
        File.WriteAllText(path,json);
        Debug.Log("保存成功 ：" +fileName +" 位置:"+path);
    } 
    

}
